<?php

namespace App\Models;

use App\Models\base\FengtModel;
use App\Models\base\WarehouseModel;
use App\Models\dispatch\CarModel;
use App\Models\dispatch\DriverModel;
use App\Models\dispatch\ExceptionModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class DispatchTaskModel extends BaseModel
{
    use HasFactory, SoftDeletes;

    protected $table = 't_dispatch_task';

    protected $primaryKey = 'id';

    public $timestamps = true;

    const CREATED_AT = 'created_at';
    
    const UPDATED_AT = null;

    protected $guarded = [];

    protected $hidden = [
        'deleted_at',
    ];

    //状态 1待运输 2运输中 3已完成
    const STATUS_WAIT = 1;
    const STATUS_TRANSPORTING = 2;
    const STATUS_FINISHED = 3;
    const STATUS_MAPS = [
        self::STATUS_WAIT => '待运输',
        self::STATUS_TRANSPORTING => '运输中',
        self::STATUS_FINISHED => '已完成'
    ];

    //类型 1港车 2内地 3其他
    const TYPE_PORT = 1;
    const TYPE_MAINLAND = 2;
    const TYPE_OTHER = 3;
    const TYPE_MAPS = [
        self::TYPE_PORT => '港车',
        self::TYPE_MAINLAND => '内地',
        self::TYPE_OTHER => '其他'
    ];

    const NODE_STATUS_OUT = 'out';
    const NODE_STATUS_DECLARE = 'declare';
    const NODE_STATUS_CHECK = 'check';
    const NODE_STATUS_PASS = 'pass';
    const NODE_STATUS_MAPS = [
        self::NODE_STATUS_OUT => '出库',
        self::NODE_STATUS_DECLARE => '报关',
        self::NODE_STATUS_CHECK => '查验',
        self::NODE_STATUS_PASS => '放行'
    ];

    //装车表  1未录入 2已录入 
    const LOAD_WAIT = 1;
    const LOAD_FINISHED = 2;
    const LOAD_MAPS = [
        self::LOAD_WAIT => '未录入',
        self::LOAD_FINISHED => '已录入'
    ];


    //代理商
    public function agent(): BelongsTo {
        return $this->belongsTo(AgentModel::class, 'agent_id');
    }

    //封关场
    public function feng(): BelongsTo {
        return $this->belongsTo(FengtModel::class, 'feng_id');
    }

    //装车货站
    public function load_warehouse(): BelongsTo {
        return $this->belongsTo(WarehouseModel::class, 'load_warehouse_id');
    }

    //经停地
    public function pass_warehouse(): BelongsTo {
        return $this->belongsTo(WarehouseModel::class, 'pass_warehouse_id');
    }

    //卸货站
    public function unload_warehouse(): BelongsTo {
        return $this->belongsTo(WarehouseModel::class, 'unload_warehouse_id');
    }

    //大陆司机
    public function driver(): BelongsTo {
        return $this->belongsTo(DriverModel::class, 'driver_id');
    }

    //香港司机
    public function hk_driver(): BelongsTo {
        return $this->belongsTo(DriverModel::class, 'hk_driver_id');
    }

    //车辆
    public function car(): BelongsTo {
        return $this->belongsTo(CarModel::class, 'car_id');
    }

    //创建人
    public function create_admin(): BelongsTo {
        return $this->belongsTo(AdminModel::class, 'created_admin_id');
    }

    //装车列表
    public function load_list(): HasMany {
        return $this->hasMany(DispatchLoadListModel::class, 'dispatch_task_id');
    }

    //运输成本
    public function costs(): HasMany {
        return $this->hasMany(DispatchCostModel::class, 'dispatch_task_id');
    }

    //装货地
    public function loads(): HasMany {
        return $this->hasMany(DispatchTaskLoadModel::class, 'dispatch_task_id');
    }

    //卸货地
    public function unloads(): HasMany {
        return $this->hasMany(DispatchTaskUnloadModel::class, 'dispatch_task_id');
    }

    //车辆类型
    public function type(): BelongsTo {
        return $this->belongsTo(DictionaryModel::class, 'type_id');
    }

    //异常
    public function exception(): HasMany {
        return $this->hasMany(ExceptionModel::class, 'task_id');
    }
}
